package com.lovi.io_.homework;

import java.io.*;
import java.util.Properties;

/**
 * (1)要编写一个dog.properties
 * name=tom
 * age=5
 * color=red
 * (2)编写Dog 类(name,age,color)创建一个dog对象，
 * 读取dog.properties 用相应的内容完成属性初始化,并输出
 * (3)将创建的Dog 对象 ，序列化到 文件 dog.dat 文件
 */
public class Test03 {
    public static void main(String[] args) throws IOException, ClassNotFoundException {
        Properties properties = new Properties();
        String filePath = "D:\\Documents\\coding_leaning\\coding_workspace\\java_document\\韩顺平900集循序渐进学Java\\Java-03\\src\\main\\resources\\dog.properties";
        properties.load(new FileInputStream(filePath));
        properties.list(System.out);
        System.out.println("------------");
        String name = (String) properties.get("name");
        Integer age = Integer.parseInt((String) properties.get("age"));
        String color = (String) properties.get("color");
        Dog dog = new Dog(name, age, color);
        System.out.println(dog);

        String filePath2 = "D:\\桌面\\test\\dog.dat";
        ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(filePath2));
        oos.writeObject(dog);
        oos.close();
        System.out.println("dog成功序列化写入！");
        System.out.println("反序列化拿到dog数据");

        //反序列化拿到dog数据
        ObjectInputStream ois = new ObjectInputStream(new FileInputStream(filePath2));
        Object dog2 = ois.readObject();
        System.out.println("反序列化得到的dog信息："+dog2);
        ois.close();

    }
}

class Dog implements Serializable {
    private String name;
    private int age;
    private String color;

    public Dog(String name, int age, String color) {
        this.name = name;
        this.age = age;
        this.color = color;
    }

    @Override
    public String toString() {
        return "Dog{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", color='" + color + '\'' +
                '}';
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getColor() {
        return color;
    }

    public void setColor(String color) {
        this.color = color;
    }
}
